package com.yiren.dbaa.module.practice_set.line.view.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.dbaa.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author lwb
 * @date : 2022/11/17 10:18
 * @description: 共享线路适配器
 */
public class ShareLineAdapter extends BaseAdapter {

    private Context context;
    private List<LineInfoEntity> list;

    public ShareLineAdapter(Context context, List<LineInfoEntity> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = View.inflate(context, R.layout.item_share_line, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        LineInfoEntity lineInfo = list.get(position);
        viewHolder.tvName.setText(lineInfo.getName());
        viewHolder.tvPoints.setText("点位数:" + lineInfo.getPointNumber());

        if (lineInfo.getLineType() == 1) {
            viewHolder.tvType.setText("考试路段");
            viewHolder.vBar3.setVisibility(View.GONE);
            viewHolder.tvLocation.setVisibility(View.GONE);
        } else {
            viewHolder.tvType.setText("训练路段");
            viewHolder.tvLocation.setText(lineInfo.getLocation());
        }
        viewHolder.tvUser.setText(lineInfo.getUserId() + "");

        if (lineInfo.isDownLoad()) {
            viewHolder.tvDownload.setBackground(context.getResources().getDrawable(R.drawable.btn_gray_select_shape));
            viewHolder.tvDownload.setText("已下载");
        }else {
            viewHolder.tvDownload.setText("  下载  ");
        }

        viewHolder.tvPreview.setOnClickListener(v -> {
            if (mOnPreviewClickListener != null) {
                mOnPreviewClickListener.onPreviewClick(position);
            }
        });
        viewHolder.tvDownload.setOnClickListener(v -> {
            if (mOnDownloadClickListener != null && !lineInfo.isDownLoad()) {
                mOnDownloadClickListener.onDownloadClick(position);
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_points)
        TextView tvPoints;
        @BindView(R.id.tv_type)
        TextView tvType;
        @BindView(R.id.tv_user)
        TextView tvUser;
        @BindView(R.id.v_bar3)
        View vBar3;
        @BindView(R.id.tv_location)
        TextView tvLocation;
        @BindView(R.id.tv_preview)
        TextView tvPreview;
        @BindView(R.id.tv_download)
        TextView tvDownload;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
    //条目预览回调
    public interface OnPreviewClickListener {
        void onPreviewClick(int position);
    }

    private OnPreviewClickListener mOnPreviewClickListener;

    public void setOnPreviewClickListener(OnPreviewClickListener onPreviewClickListener) {
        mOnPreviewClickListener = onPreviewClickListener;
    }

    //下载点击回调
    public interface OnDownloadClickListener {
        void onDownloadClick(int position);
    }

    private OnDownloadClickListener mOnDownloadClickListener;

    public void setOnDownloadClickListener(OnDownloadClickListener onDownloadClickListener) {
        mOnDownloadClickListener = onDownloadClickListener;
    }

}